#pragma once
#include <string>
#include <jsoncpp/json/json.h>
using namespace std;

namespace ns_protocol
{
    class Request
    {
    public:
        //序列化
        string Serialization()
        {
            Json::Value root;
            root["code"] = _code;
            root["cpu_limit"] = _cpu_limit;
            root["mem_limit"] = _mem_limit;

            Json::FastWriter writer;
            return writer.write(root);
        }

        // 反序列化
        bool DeSerialization(const string &in_json)
        {
            Json::Value root;
            Json::Reader reader;
            if (reader.parse(in_json, root))
            {
                _code = root["code"].asString();
                _cpu_limit = root["cpu_limit"].asInt();
                _mem_limit = root["mem_limit"].asInt();
                return true;
            }
            
            return false;
        }

        const string* Code()
        {
            return &_code;
        }

        int CpuLimit()
        {
            return _cpu_limit;
        }

        int MemLimit()
        {
            return _mem_limit;
        }

        void Set(const string& code, int cpu, int mem)
        {
            _code = code;
            _cpu_limit = cpu;
            _mem_limit = mem;
        }

    private:
        string _code; // src代码
        //资源限制
        int _cpu_limit;
        int _mem_limit;
        //......
    };

    class Response
    {
    public:
        //序列化
        string Serialization()
        {
            Json::Value root;
            root["status"] = _status;
            root["desc"] = _desc;
            root["stdout"] = _stdout;

            Json::FastWriter writer;
            return writer.write(root);
        }

        //反序列化
        bool DeSerialization(const string& in_json)
        {
            Json::Value root;
            Json::Reader reader;
            if (reader.parse(in_json, root))
            {
                _status = root["status"].asInt();
                _desc = root["desc"].asString();
                _stdout = root["stdout"].asString();
                return true;
            }
            
            return false;
        }

        string ToString()
        {
            string ret = "Status: " + to_string(_status) + "\n" + "Desc: " + _desc + "\n\n" + _stdout;
            return ret;
        }

        //设置
        void ReSet(int stat, const string& desc, const string& Stdout = string())
        {
            _status = stat;
            _desc = desc;
            _stdout = Stdout;
        }

        //缺省构造
        Response(int stat = -1, const string& desc = "Internal system error.")
        :_status(stat), _desc(desc), _stdout(string())
        {}

    private:
        int _status;//结果状态
        string _desc;//状态描述

        string _stdout;
    };
}